/* Form_Calculation.cs - View - Berechnungsfenster
 * Copyright (C) 2009 Dietmar Schinnerl
 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License along with this program; if not, see <http://www.gnu.org/licenses/>.
 */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace App.Net
{
    public partial class Form_Calculation : Form
    {
        public Calculation Calculation;
        public AppProject AppProject;
        public int BestError;
        private int lastIterationOfNewBestError;
        public Form_Calculation()
        {
            BestError = 0;
            InitializeComponent();

         

        }

        public void Run()
        {
            backgroundWorkerCalculation.RunWorkerAsync();

        }

        private void backgroundWorkerCalculation_DoWork(object sender, DoWorkEventArgs e)
        {
            // Get the BackgroundWorker that raised this event.
            BackgroundWorker worker = sender as BackgroundWorker;


            //treeTasks.SelectedNode = null;
            Calculation = new Calculation(AppProject, worker);

            Calculation.Calculate();
            while ((worker.CancellationPending == false) && (Calculation.BestError>0))
            {
                try
                {
                    Calculation.CalculateIterations(10);
                }
                catch (Exception ex)
                {
                    worker.ReportProgress(-1);
                    MessageBox.Show(ex.Message, "Error");
                    return;
                }
                worker.ReportProgress(0);
            }
        }

        private void backgroundWorkerCalculation_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (Calculation.BestError == Calculation.BestErrorStartValue)
                this.Close();
            //GenerateOutputCalculationOverview();
            pictureBoxCalculation.Refresh();
            DisplayCalculationOutput.BestResultStatsRichText(Calculation,richTextCalculation,true);
            btnCancel.Text = "Close Window";
            pbPower.Value = 0;
        }

        private void backgroundWorkerCalculation_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            if (e.ProgressPercentage == -1)
            {
                pbError.Value = 100;
                pbError.ForeColor = Color.Red;
                pbPower.Value = 100;
                pbPower.ForeColor = Color.Red;
                return;
            }

            const int timeShownAs100=5000;
            int time=Calculation.Iteration-lastIterationOfNewBestError;
            int shownPower=((time - timeShownAs100) + timeShownAs100) *100 / timeShownAs100;;
            if (shownPower > 100)
                shownPower = 100;
            if (shownPower < 0)
                shownPower = 0;
            pbPower.Value = 100-shownPower;


            if (Calculation.BestError != BestError)
            {
                labelError.Text = "Error: " + Calculation.BestError.ToString();
                
                lastIterationOfNewBestError = Calculation.Iteration;
                //int errorDays=System.Math.Sqrt(calculation.bestError);
                const int errorShownAs100 = 5000;
                int shownError = ((Calculation.BestError - errorShownAs100) + errorShownAs100) *100 / errorShownAs100;
                if (shownError>100)
                    shownError=100;
                if (shownError < 0)
                    shownError = 0;
                //shownError = 100 - shownError;
                if ((Calculation.BestError > 0) && (shownError == 0)) shownError = 1;


                pbError.Value=shownError;
                pictureBoxCalculation.Refresh();
                //calculationInfo.Text = "Iteration: " + calculation.iteration.ToString() + "   Error: " + calculation.bestError.ToString() + "           ";
                DisplayCalculationOutput.BestResultStatsRichText(Calculation, richTextCalculation,true);
                BestError = Calculation.BestError;
            }
        }









        private void Form_Calculation_FormClosing(object sender, FormClosingEventArgs e)
        {
            backgroundWorkerCalculation.CancelAsync();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            backgroundWorkerCalculation.CancelAsync();
            this.Close();
        }

        private void picturepictureBoxCalculation_Paint(object sender, PaintEventArgs e)
        {
            
            DisplayCalculationOutput.BestResultDefaultGraphics(Calculation, e.Graphics, pictureBoxCalculation);
        }

        private void pictureBoxCalculation_SizeChanged(object sender, EventArgs e)
        {
            pictureBoxCalculation.Refresh();
        }



    }
}